<!--
 * @Author: Lian
 * @Date: 2022-05-06 09:31:23
 * @LastEditors: Lian
 * @LastEditTime: 2022-05-10 15:31:41
 * @Description: 2048
-->
<template>
  <view></view>
</template>

<script>
export default {
  data () {
    return {
      checkerboard: [    // 棋盘
        [0, 2, 2, 0],
        [0, 0, 4, 0],
        [2, 0, 0, 2],
        [2, 4, 4, 0]
      ]
    };
  },
  onShow () {
    this.move_right()
  },
  methods: {
    // 获取位置为空的坐标
    getNull (board) {
      let nullList = []
      for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
          board[i][j] == 0 && nullList.push([i, j])
        }
      }
      return nullList
    },

    // 在空位置随机生成2或者4(2的几率比较大)
    createNum () {
      const nums = [2, 2, 4]
      const nullList = this.getNull(this.checkerboard)
      const null_num = nullList[Math.floor(Math.random() * nullList.length)]
      const nums_num = Math.floor(Math.random() * nums.length)
      this.checkerboard[null_num[0]][null_num[1]] = nums[nums_num]
    },

    // 左滑操作
    move_left () {
      for (let item = 0; item < 4; item++) {
        let column = this.checkerboard[item]
        for (let k = 0; k < 4; k++) {
          for (let i = k + 1; i < 4; i++) {
            if (column[i] == 0) {
              continue
            } else if (column[k] == 0) {
              column[k] = column[i]
              column[i] = 0
            } else if (column[k] == column[i]) {
              column[k] = column[k] * 2
              column[i] = 0
              break
            } else {
              if (k + 1 != i) {
                column[k + 1] = column[i]
                column[i] = 0
              }
              break
            }
          }
        }
      }
    },

    // 右滑操作
    move_right () {
      for (let item = 0; item < 4; item++) {
        let column = this.checkerboard[item]
        for (let k = 3; k >= 0; k--) {
          for (let i = k - 1; i >= 0; i--) {
            if (column[i] == 0) {
              continue
            } else if (column[k] == 0) {
              column[k] = column[i]
              column[i] = 0
            } else if (column[k] == column[i]) {
              column[k] = column[k] * 2
              column[i] = 0
              break
            } else {
              if (k - 1 != i) {
                column[k - 1] = column[i]
                column[i] = 0
              }
              break
            }
          }
        }

      }
    },

    // 上滑操作
    move_up () {
      let column = [2, 0, 0, 2]
      for (let k = 0; k < 4; k++) {
        for (let i = k + 1; i < 4; i++) {
          if (column[i] == 0) {
            continue
          } else if (column[k] == 0) {
            column[k] = column[i]
            column[i] = 0
          } else if (column[k] == column[i]) {
            column[k] = column[k] * 2
            column[i] = 0
            break
          } else {
            if (k + 1 != i) {
              column[k + 1] = column[i]
              column[i] = 0
            }
            break
          }
        }
      }
      console.log(column);
    },

    // 下滑操作
    move_bottom () {
      let column = [2, 0, 0, 2]
      for (let k = 0; k < 4; k++) {
        for (let i = k + 1; i < 4; i++) {
          if (column[i] == 0) {
            continue
          } else if (column[k] == 0) {
            column[k] = column[i]
            column[i] = 0
          } else if (column[k] == column[i]) {
            column[k] = column[k] * 2
            column[i] = 0
            break
          } else {
            if (k + 1 != i) {
              column[k + 1] = column[i]
              column[i] = 0
            }
            break
          }
        }
      }
      console.log(column);
    }
  }
}
</script>

<style lang="scss">
</style>
